/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package query;

/**
 *
 * @author Gonzalo
 */
public class Parser {

    public Parser(){}

    public Debug getDebug(String consulta){
        String t[]=consulta.trim().split(" ");
        return new Debug(t[1]);
    }

    public DDL getDDL(String consulta){
        String t[]=consulta.trim().split(" ");
        return new DDL(t[0],t[1],t[2]);
    }
    public DML getDML(String consulta){
        String t[]=consulta.trim().split(" ");
        if(t[0].compareTo("new")==0){
            if(t[1].compareTo("node")==0)
                return new DML(t[0],t[1]);
            else
                return new DML(t[0],t[1],Long.parseLong(t[2]),Long.parseLong(t[3]));
        }else if(t[0].compareTo("set")==0){
            return new DML(t[0],t[1],t[2],Long.parseLong(t[3]));
        }else { //if(t[0].compareTo("del")==0)
            return new DML(t[0],t[1],Long.parseLong(t[2]),Long.parseLong(t[3]));
        }
    }

    public boolean isDDL(String _consulta){
        String consulta=_consulta.trim();
        if(consulta.startsWith("def "))
            return true;
        return false;
    }
    public boolean isDML(String _consulta){
        String consulta=_consulta.trim();
        if(consulta.startsWith("new ") || consulta.startsWith("set ") || consulta.startsWith("del "))
            return true;
        return false;        
    }
    public boolean isDebug(String _consulta){
        String consulta=_consulta.trim();
        if(consulta.startsWith("debug "))
            return true;
        return false;
    }

    public Query getQuery(String _consulta){
        try{
        String consulta=quitarParentesis(_consulta.trim());
        String izq="";
        String der="";
        int parentesis=0;
        char c;
        for(int k=0;k<consulta.length();k++){
            c=consulta.charAt(k);
            izq=consulta.substring(0, k);
            der=consulta.substring(k+1);
            if(isParentesisIzq(c))
                parentesis++;
            if(isParentesisDer(c))
                parentesis--;
            if(parentesis==0){
                Operation op=new Operation(""+c);
                if(op.isUnary() && k==consulta.length()-1){
                    return new Query(op,getQuery(izq));
                }if(op.isBinary()){
                    return new Query(op,getQuery(izq),getQuery(der));
                }
            }
        }
        if(isCondicionDer(consulta.charAt(consulta.length()-1))){
            izq="";
            der="";
            for(int k=0;k<consulta.length();k++){
                c=consulta.charAt(k);
                if(isCondicionIzq(c)){
                    izq=consulta.substring(0,k);
                    der=consulta.substring(k+1,consulta.length()-1);
                    return new Query(getPredicado(der));
                }
            }
        }
        return new Query(new Predicate(consulta));

        } catch(Exception e){
             return new Query();
        }
    }

    public Predicate getPredicado(String _condicion){
        String condicion=_condicion.trim();
        String izq="";
        String der="";
        char c;
        if(condicion.compareTo("T")==0)
            return new Predicate(true);
        if(condicion.compareTo("F")==0)
            return new Predicate(false);
        try{
            return new Predicate(Long.parseLong(condicion));
        }catch(Exception e){

            for(int k=0;k<condicion.length();k++){
                c=condicion.charAt(k);
                izq=condicion.substring(0, k).trim();
                der=condicion.substring(k+1).trim();
                if(isIgual(c))
                    return new Predicate(izq,c+"",der);
            }
            return new Predicate(condicion);
        }
    }

    public String quitarParentesis(String _consulta){
        int parentesis=0;
        char c;
        if(isParentesisIzq(_consulta.charAt(0)) && isParentesisDer(_consulta.charAt(_consulta.length()-1))){
            String consulta=_consulta.substring(1,_consulta.length()-1).trim();
            for(int k=0;k<consulta.length();k++){
                c=consulta.charAt(k);
                if(isParentesisIzq(c))
                    parentesis++;
                if(isParentesisDer(c))
                    parentesis--;
                if(parentesis<0)
                    return _consulta;
            }
            if(parentesis==0)
                    return quitarParentesis(consulta.trim());
        }
        return _consulta;
    }

    public boolean isParentesisIzq(char op){
        if(op=='(')
            return true;
        else
            return false;
    }

    public boolean isParentesisDer(char op){
        if(op==')')
            return true;
        else
            return false;
    }

    public boolean isCondicionIzq(char op){
        if(op=='[')
            return true;
        else
            return false;
    }

    public boolean isCondicionDer(char op){
        if(op==']')
            return true;
        else
            return false;
    }

    public boolean isIgual(char op){
        if(op=='=')
            return true;
        else
            return false;
    }
}
